<?php
class Categories_Model {
	public $con;
	public function __construct(){
		$this->con =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		if(mysqli_connect_error())
		{
			echo "Failed to connect to MySQL". mysqli_connect_error();exit();
		}	
	}
	public function getListCate() {
		$query = "SELECT c.id,c.nameCat,c.description FROM categories AS c";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function viewCat($id)
	{
		$query = "SELECT * FROM categories WHERE id='$id'";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function addCat($nameCat,$description) {
		$query = "INSERT INTO categories(nameCat,description) VALUE ('$nameCat','$description')";
		$result = mysqli_query($this->con,$query);
	}

	public function editCat($nameCat,$description,$id) {
		$query = "UPDATE categories SET nameCat = '$nameCat',description = '$description' WHERE id = '$id'";
		$result = mysqli_query($this->con,$query);
	}

	public function beforeditCat($id) {
		$query = "SELECT * FROM categories WHERE id=".$id;
		$result = mysqli_query($this->con,$query);
		return $result;
	}

	public function delCat($id) {
		$query = "DELETE FROM categories WHERE id=".$id;
		$result = mysqli_query($this->con,$query);
	}
}
?>